package me.mononin.dilechat.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import me.mononin.common.common.app.Activity;
import me.mononin.common.common.app.Fragment;
import me.mononin.common.factory.model.Author;
import me.mononin.dilechat.R;
import me.mononin.dilechat.fragments.message.ChatGroupFragment;
import me.mononin.dilechat.fragments.message.ChatUserFragment;
import me.mononin.factory.model.db.Group;
import me.mononin.factory.model.db.Message;
import me.mononin.factory.model.db.Session;

public class MessageActivity extends Activity {

    public static final String RECEIVER_ID = "RECEIVER_ID";

    public static final String RECEIVER_IS_GROUP = "RECEIVER_IS_GROUP";

    private String mReceiverId;
    private boolean mIsGroup;

    /**
     * 通过发起聊天上下文
     *
     * @param context 上下文
     * @param session  人的信息
     */
    public static void show(Context context, Session session) {
        if (context == null || session == null
                || TextUtils.isEmpty(session.getId())) {
            return;
        }

        Intent intent = new Intent(context, MessageActivity.class);
        intent.putExtra(RECEIVER_ID, session.getId());
        intent.putExtra(RECEIVER_IS_GROUP, session.getReceiverType()== Message.RECEIVER_TYPE_GROUP);
        context.startActivity(intent);
    }

    /**
     * 显示人的聊天界面
     *
     * @param context 上下文
     * @param author  人的信息
     */
    public static void show(Context context, Author author) {
        if (context == null || author == null
                || TextUtils.isEmpty(author.getId())) {
            return;
        }

        Intent intent = new Intent(context, MessageActivity.class);
        intent.putExtra(RECEIVER_ID, author.getId());
        intent.putExtra(RECEIVER_IS_GROUP, false);
        context.startActivity(intent);
    }

    /**
     * 显示群组聊天界面
     * @param context 上下文
     * @param group 群信息
     */
    public static void show(Context context, Group group) {
        if (context == null || group == null
                || TextUtils.isEmpty(group.getId())) {
            return;
        }

        Intent intent = new Intent(context, MessageActivity.class);
        intent.putExtra(RECEIVER_ID, group.getId());
        intent.putExtra(RECEIVER_IS_GROUP, true);
        context.startActivity(intent);
    }

    @Override
    protected int getContentId() {
        return R.layout.activity_message;
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        mReceiverId = bundle.getString(RECEIVER_ID);
        mIsGroup = bundle.getBoolean(RECEIVER_IS_GROUP);
        //接收对象Id不能为空
        return !TextUtils.isEmpty(mReceiverId);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle("");
        Fragment fragment;
        if (mIsGroup){
            fragment = new ChatGroupFragment();
        }else {
            fragment = new ChatUserFragment();
        }

        Bundle bundle = new Bundle();
        bundle.putString(RECEIVER_ID,mReceiverId);
        fragment.setArguments(bundle);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.lay_container,fragment)
                .commit();
    }
}
